package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.text.format.Formatter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.webbrowser.OaGameActivity;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.LocalGmaeInfo;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.service.HttpWebCoreService;
import com.rjwh.dingdong.client.util.FileUtils;
import com.rjwh.dingdong.client.util.ToastUtil;
import com.rjwh.dingdong.client.util.ViewHolder;
import com.wby.base.AdapterBase;

import java.io.File;

/**
 * 项目名称：whrjwh-parent
 * 类描述：
 * 创建人：wby
 * 创建时间：2016/6/27 11:01
 * 修改人：wby
 * 修改时间：2016/6/27 11:01
 * 修改备注：
 *
 */
public class DowloadLocaListAdapter extends AdapterBase<LocalGmaeInfo> {
    private Context context;

    public DowloadLocaListAdapter(Context context) {
        this.context = context;
    }


    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    protected View getExView(int position, View convertView, ViewGroup parent) throws Exception {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.dowload_local_list_item, null);
        }

        LinearLayout headView = ViewHolder.get(convertView, R.id.download_local_head_layout);
        TextView headText = (TextView) convertView.findViewById(R.id.download_local_head_typename_tv);
        ImageView img = ViewHolder.get(convertView, R.id.download_local_img_pic);
        TextView tvAppNmae = ViewHolder.get(convertView, R.id.download_local_appname_tv);
        TextView tvDownloadSize = ViewHolder.get(convertView, R.id.download_manager_downloadSize_tv);
        Button button = ViewHolder.get(convertView, R.id.download_local_start_btn);

        final LocalGmaeInfo mData = (LocalGmaeInfo) getItem(position);
        if (mData.getType() == 1) {
            button.setVisibility(View.VISIBLE);
            button.setText("开始阅读");
        } else if (mData.getType() == 2) {
            button.setVisibility(View.VISIBLE);
            button.setText("开始游戏");
        } else {
            button.setVisibility(View.GONE);
        }
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mData.getType() == 1) {
                    File file = new File(FileUtils.getDrwabookDirDirectory(), mData.getPid() + "/");
                    if (file.isDirectory() && file.listFiles().length > 0) {
                        String userid = MyApplication.spUtil.getStrPreferenceByParamName(context, LocalConstant.SP_USERID);
                        String babyid = MyApplication.spUtil.getStrPreferenceByParamName(context, LocalConstant.SP_BABYID);
                        String token = MyApplication.spUtil.getStrPreferenceByParamName(context, LocalConstant.SP_USER_TOKEN);
                        String uuid = MyApplication.deviceId;
                        String version = MyApplication.versionCode;
                        String url = String.format(HttpWebCoreService.HUIBEN_URL, mData.getPid(), userid,
                                babyid, version, uuid, token, System.currentTimeMillis(), mData.getPid());
                        OaGameActivity.startActivity(context, url);
                    } else {
                        ToastUtil.showToast(context, "路径错误");
                    }
                } else {
                    File file = new File(FileUtils.getGameDirectory(), mData.getPid() + "/");
                    if (file.isDirectory() && file.listFiles().length > 0) {
                        String userid = MyApplication.spUtil.getStrPreferenceByParamName(context, LocalConstant.SP_USERID);
                        String babyid = MyApplication.spUtil.getStrPreferenceByParamName(context, LocalConstant.SP_BABYID);
                        String token = MyApplication.spUtil.getStrPreferenceByParamName(context, LocalConstant.SP_USER_TOKEN);
                        String uuid = MyApplication.deviceId;
                        String version = MyApplication.versionCode;
                        String url = String.format(HttpWebCoreService.GAME_URL, mData.getPid(), userid,
                                babyid, version, uuid, token, System.currentTimeMillis(), mData.getPid());
                        OaGameActivity.startActivity(context, url);
                    } else {
                        ToastUtil.showToast(context, "路径错误");
                    }
                }

            }
        });
        Glide.with(context).load(mData.getPic()).placeholder(R.drawable.icon_pic_holder).error(R.drawable.icon_pic_holder).dontAnimate().into(img);
        tvAppNmae.setText(mData.getPname());
        String totalLength = Formatter.formatFileSize(context,mData.getTotalLength());
        tvDownloadSize.setText(totalLength);

        if (mData.getType() == 1) {
            headText.setText("绘本");
        } else if (mData.getType() == 2) {
            headText.setText("游戏");
        } else {
            headText.setText("绘本公共资源");
        }

        if (position > 0) {
            int lastType = mList.get(position - 1).getType();
            if (lastType == mData.getType()) {
                headView.setVisibility(View.GONE);
            } else {
                headView.setVisibility(View.VISIBLE);
            }
        } else {
            headView.setVisibility(View.VISIBLE);
        }

        return convertView;


    }

    @Override
    protected void onReachBottom() {

    }

    public void delItme(int ps) {
        getList().remove(ps);
        notifyDataSetChanged();
    }

}
